package com.puman.watchtrade.util;

import java.io.File;
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

import com.puman.watchtrade.MainActivity;
import com.puman.watchtrade.R;
import com.puman.watchtrade.fragment.home.model.CustomDigitalClock;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class Gjfun {

	public static DecimalFormat moneyDF = new DecimalFormat("#.##");
	public static DecimalFormat oneDF = new DecimalFormat("#.#");
	private static SimpleDateFormat df = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");// ��־�������ʽ

	public static void sleep(long s) {
		try {
			Thread.sleep(s);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static String byteToHexString(byte[] bArray, final int size) {

		// StringBuffer sb = new StringBuffer(bArray.length + bArray.length/2);
		StringBuffer sb = new StringBuffer(size + size / 2);
		String sTemp;
		// for (int i = 0; i < bArray.length; i++) {
		for (int i = 0; i < size; i++) {
			sTemp = Integer.toHexString(0xFF & bArray[i]);
			if (sTemp.length() < 2)
				sb.append(0);
			sb.append(sTemp.toUpperCase());
			sb.append(' ');
		}
		return sb.toString();
	}

	public static String byteToHexString(byte[] bArray) {
		int size = bArray.length;
		// StringBuffer sb = new StringBuffer(bArray.length + bArray.length/2);
		StringBuffer sb = new StringBuffer(size + size / 2);
		String sTemp;
		// for (int i = 0; i < bArray.length; i++) {
		for (int i = 0; i < size; i++) {
			sTemp = Integer.toHexString(0xFF & bArray[i]);
			if (sTemp.length() < 2)
				sb.append(0);
			sb.append(sTemp.toUpperCase());
			sb.append(' ');
		}
		return sb.toString();
	}

	public static String getNowDateTime() {
		Date nowtime = new Date();
		return df.format(nowtime);
	}

	public static String isDoubleNum(int num) {
		if (num < 10)
			return "0" + num;
		return num + "";

	}

	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float ratio) {

		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		final RectF rectF = new RectF(rect);

		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		canvas.drawRoundRect(rectF, bitmap.getWidth() / ratio,
				bitmap.getHeight() / ratio, paint);

		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
		return output;
	}

	public static boolean isNetworkConnected(Context context) { // �����Ƿ�δ����
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mNetworkInfo = mConnectivityManager
					.getActiveNetworkInfo();
			if (mNetworkInfo != null) {
				// return mNetworkInfo.isAvailable();
				return false;
			}
		}
		return true;
	}

	public static boolean checkFileExists(String filepath) {
		File file = new File(Environment.getExternalStorageDirectory()
				+ filepath);
		return file.exists();
	}

	public static String[] strToArray(String str) {
		String[] stringArr = null;
		if (str != null)
			stringArr = str.split("&");

		return stringArr;

	}

	public static String[] strToArrayByN(String str) {
		String[] stringArr = null;
		if (str != null)
			stringArr = str.split("\r\n");

		return stringArr;

	}

	// ��ϸ��ַת�ɳ�ʡ
	public static String detailToPro(String str) {
		String proVenci = "";

		// �Ƚ���ֱϽ��
		if (str.contains("������"))
			proVenci = "������";
		else if (str.contains("�Ϻ���"))
			proVenci = "�Ϻ���";
		else if (str.contains("�����"))
			proVenci = "�����";
		else if (str.contains("������"))
			proVenci = "������";
		else {

			int index = str.indexOf("ʡ");
			proVenci = str.substring(0, index + 1);

		}

		return proVenci;

	}

	/**
	 * ����ֻ�ķֱ��ʴ� dp �ĵ�λ ת��Ϊ px(����)
	 */
	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	/**
	 * ����ֻ�ķֱ��ʴ� px(����) �ĵ�λ ת��Ϊ dp
	 */
	public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}

	// ��ȡ�ֻ�״̬���߶�
	public static int getStatusBarHeight(Context context) {
		Class<?> c = null;
		Object obj = null;
		Field field = null;
		int x = 0, statusBarHeight = 0;
		try {
			c = Class.forName("com.android.internal.R$dimen");
			obj = c.newInstance();
			field = c.getField("status_bar_height");
			x = Integer.parseInt(field.get(obj).toString());
			statusBarHeight = context.getResources().getDimensionPixelSize(x);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return statusBarHeight;
	}

	/*
	 * �ƶ���134��135��136��137��138��139��150��151��152��157��158��159��182��183�
	 * �184��187��188��178(4G)��147(����)
	 * ��ͨ��130��131��132��155��156��185��186��176(4G)��145(����)
	 * ���ţ�133��153��180��181��189 ��177(4G) ����ͨ�ţ�1349 ������Ӫ�̣�170
	 */

	public static boolean isPhoneNumberValid(String mobiles) {

		String telRegex = "[1][34578]\\d{9}";// "[1]"����1λΪ����1��"[358]"���ڶ�λ����Ϊ3��5��8�е�һ����"\\d{9}"�������ǿ�����0��9�����֣���9λ��
		if (TextUtils.isEmpty(mobiles))
			return false;
		else
			return mobiles.matches(telRegex);
	}

	public static LoadingProgressDialog startProgressDialog(
			LoadingProgressDialog mProgress, Activity activity) {
		if (mProgress == null) {
			mProgress = LoadingProgressDialog.createDialog(activity);
			mProgress.setMessage("");
		}
		mProgress.show();
		return mProgress;
	}

	public static void stopProgressDialog(LoadingProgressDialog mProgress) {
		if (mProgress != null) {
			mProgress.dismiss();
			mProgress = null;
		}
	}

	/**
	 * 数据为空时的默认布局 curry 201506
	 * 
	 * @param mView
	 *            当前界面
	 * @param info
	 *            提示信息
	 * @param isShow
	 *            是否显示
	 */
	public static void emptyLayout(View mView, String info, String endtime,
			boolean isShow) {
		if (isShow) {
			mView.findViewById(R.id.load_empty_layout_info).setVisibility(
					View.VISIBLE);
			if (null != info) {
				((TextView) mView.findViewById(R.id.load_default_info))
						.setText(info);
				if (endtime.equals("")) {
					DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					try {
						Date d1 = df.parse("2015-08-30 20:00:00");
						CustomDigitalClock clock;
						clock = (CustomDigitalClock) mView
								.findViewById(R.id.load_default_time);
						clock.setEndTime(System.currentTimeMillis()+d1.getTime());
						clock.setClockListener(new CustomDigitalClock.ClockListener() { // register
							@Override
							public void timeEnd() {
								// The clock time is ended.
							}

							@Override
							public void remainFiveMinutes() {
								// The clock time is remain five minutes.
							}
						});
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
			}
		} else {
			mView.findViewById(R.id.load_empty_layout_info).setVisibility(
					View.GONE);
		}
	}

}
